package org.sky.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * @author weifx
 * @Title: ServiceException
 * @ProjectName springboot-platform
 * @Description: TODO
 * @date 2019/3/6 17:45
 * @Version 1.0
 */
public class ServiceException extends RuntimeException {
    private Throwable rootCause;

    public ServiceException(String s) {
        super(s);
    }

    public ServiceException(String s, Throwable ex) {
        super(s);
        rootCause = ex;
    }

    public Throwable getRootCause() {
        return rootCause;
    }

    public String getMessage() {
        if (rootCause == null) {
            return super.getMessage();
        } else {
            return super.getMessage() + "; nested exception is: nt"
                    + rootCause.toString();
        }
    }

    public void printStackTrace(PrintStream ps) {
        if (rootCause == null) {
            super.printStackTrace(ps);
        } else {
            ps.println(this);
            rootCause.printStackTrace(ps);
        }
    }

    public void printStackTrace(PrintWriter pw) {
        if (rootCause == null) {
            super.printStackTrace(pw);
        } else {
            pw.println(this);
            rootCause.printStackTrace(pw);
        }
    }

    public void printStackTrace() {
        printStackTrace(System.err);
    }
}
